package com.wn.class30.service.impl;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.*;
import com.wn.class30.service.AllOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AllOrderServiceImpl implements AllOrderService {
    @Resource
    private AllorderDao allorderDao;
    @Resource
    private UserDao userDao;

    @Override
    public void deleteAllOrderById(Integer allOrderId) throws Exception {
        allorderDao.deleteByPrimaryKey(allOrderId);
    }

    @Override
    public Allorder selectAllOrderById(Integer allOrderId) throws Exception {
        return allorderDao.selectByPrimaryKey(allOrderId);
    }

    @Override
    public List<Allorder> selectAllorders(Map<String, String> map) throws Exception {
        return allorderDao.selectByExample(getAllOrderExample(map));
    }

    @Override
    public Long getAllOrderCount(Map<String, String> map) throws Exception {
        return allorderDao.countByExample(getAllOrderExample(map));
    }

    public AllorderExample getAllOrderExample(Map<String,String> map)throws Exception{
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String allorderNumber = map.get("allorderNumber");;
        String username = map.get("username");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        AllorderExample example = new AllorderExample();
        example.setLimit(limit);
        example.setOffset(page);
        AllorderExample.Criteria criteria = example.createCriteria();
        if(!"".equals(allorderNumber) && allorderNumber != null){
            criteria.andAllorderNumberLike("%"+allorderNumber+"%");
        }
        if(!"".equals(username) && username != null){
            UserExample userExample = new UserExample();
            UserExample.Criteria userExampleCriteria = userExample.createCriteria();
            userExampleCriteria.andUsernameLike("%"+username+"%");
            List<User> users = userDao.selectByExample(userExample);
            if(users.size() > 0){
                for (User user : users) {
                    criteria.andUserIdEqualTo(user.getUserId());
                }
            }else{
                criteria.andUserIdEqualTo(-1);
            }
        }

        if(!"".equals(startDate) && startDate != null){
            criteria.andAllCreatetimeGreaterThanOrEqualTo(simpleDateFormat.parse(startDate));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andAllCreatetimeLessThanOrEqualTo(simpleDateFormat.parse(format));
        }
        example.setOrderByClause("`all_createtime` DESC");
        return example;
    }
}
